#!/usr/bin/env python
# $Id$

from page import page
import sys, urllib
import db 

class add(page):

	def __init__(self):
		page.__init__(self)
		self.headingstr = "Add to www.upcdatabase.com"
		self.db1 = db.db()
		self.upcdb1 = db.upcdb()

	def render(self):
		self.logic()
		self.encoding()
		self.html()

	def logic(self):
		if request.has_key("cancel"):
			raise HTTP_REDIRECTION, "menu.py"
		if request.has_key("submit") and request["upc"][0]:
			upc = request["upc"][0]
			amt = request["amt"][0]
			desc = request["desc"][0]
			date = request["date"][0]
			count = request["count"][0]
			self.upcdb1.add_upc(upc, amt, desc)
			raise HTTP_REDIRECTION, "select_type.py?%s" % urllib.urlencode({"upc":upc,"date":date,"amt":amt,"desc":desc,"count":count})

	def form(self):
		form = """
		<form name="add" method="GET" action="add_upcdb.py">
		<input type=hidden name=upc value=%s>
		<input type=hidden name=date value=%s>
		<input type=hidden name=count value=%s>
		<table style="text-align: left; width: 20%%;" border="0" cellpadding="2" cellspacing="2">
		<tbody>
		<tr>
			<td>Description</td>
			<td><input type=text name=desc></td>
		</tr>
		<tr>
			<td>Amount and Unit</td>
			<td><input type=text name=amt></td>
		</tr>
		<tr>
			<td><input name=submit type=submit value="Submit"></td>
			<td><input type=submit name=cancel value="Cancel"></td>
		</tr>
		<script>document.forms.add.desc.focus();</script>
		</tbody>
		</table>
		</form>
		""" % (request["upc"][0], request["date"][0], request["count"][0])
		print form

	def body(self):
		self.start_body()
		self.heading()
		self.form()
		self.footer()
		self.end_body()

class runner:
	def _test(self):
		add().render()

	def _doctest(self):
		import doctest
		doctest.testmod()

if __name__ == "__main__":
	r = runner()
	getattr(r, "_"+sys.argv[1])()
else:
	add().render()
